#!/usr/bin/env python
# encoding: utf-8

"""This example shows the view filtering and how simple is to set up the
response content with Spyro.

This is a really simple example that prints a different output relying on
the request method, but you can use whatever condition to filter the view
(analyze the existence of a cookie, session value, db record and so on...)
"""

from spyro import *

class Root(WebPage):
    """Show views filtering with self.response.write and
    self.response.from_template
    
    Of course this methods could be improved to print directly the value
    of self.request.method but here we prefer to show the use of this
    value in with a if statement.
    """
    
    def index(self):
        if self.request.method == 'GET':
            self.response.write("You have requested this page via GET method.")
            self.response.write("""
<br>
<form action="/" method="POST" name="form">
<a href="javascript:document.form.submit();">POST request</a>
</form>
<br><small><a href="/template">self.response.from_template example</a></small>
""")
        elif self.request.method == 'POST':
            self.response.write("You have requested this page via POST method.")
            self.response.write("""<br><a href="/">GET request</a>
<br><small><a href="/template">self.response.from_template example</a></small>""")

    def template(self):
        if self.request.method == 'GET':
            self.response.from_template('template.html', {'method': 'GET'})
        elif self.request.method == 'POST':
            self.response.from_template('template.html', {'method': 'POST'})
